package com.xrh.core.generater.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

import com.xrh.core.generater.interfaces.MarkReplaceInf;

import lombok.AllArgsConstructor;
import lombok.Data;

public class JavaFileAttributeUtils {
	
	/**
	 * @function 从文件中读取属性
	 * @param filePath 文件绝对路径
	 * @author 肖荣辉
	 * @date 2021年7月23日
	*/
	public static MongoEntityInfo ReadAttributeFromFile(String filePath) throws Exception {
	
		File file = new File(filePath);
		
		FileInputStream fis = new FileInputStream(file);
		
		InputStreamReader isr = new InputStreamReader(fis);
		
		BufferedReader br = new BufferedReader(isr);
		
		String temp = null;
		
		//匹配注释里的文字说明
		String reg = "/\\*+([\\s\\S^\\*]*)(\\*/)$";
		Pattern  pattern = Pattern.compile(reg);
		
		String reg2 = "^//+([\\s\\S^]*)";
		Pattern  pattern2 = Pattern.compile(reg2);
		
		String reg3 = "^private\\s+([\\S]+)\\s+([\\S]+)(\\s\\S)*\\s*;$";
		Pattern  pattern3 = Pattern.compile(reg3);
		
		String reg4 = "@\\s*Document\\s*\\(\\s*((collection|value)\\s*=\\s*)*\"([\\w]+)\"([\\s\\S]*)\\)\\s*";
		Pattern  pattern4 = Pattern.compile(reg4);
		
		String reg5 = "([\\s\\S]+)\\s+class\\s+([\\w]+)([\\s\\S]*)\\{";
		Pattern  pattern5 = Pattern.compile(reg5);
		
		//上一个属性的备注
		String comment = "";
		
		List<Attribute> attributeList = new ArrayList<Attribute>();
		
		String tableName = "";
		String tableComment = "";
		String bigClassName = "";
		
		while((temp = br.readLine()) != null){
			
			temp = temp.trim();
			
			Matcher tableMatch = pattern4.matcher(temp);
			
			//读取表名和表备注
			if(StringUtils.isBlank(tableName) && tableMatch.matches()) {
				tableName = tableMatch.group(3) ;
			}
			
			//读取类名
			Matcher classNameMatch = pattern5.matcher(temp);
			if(StringUtils.isBlank(bigClassName) && classNameMatch.matches()) {
				bigClassName = classNameMatch.group(2);
				//读取到类名的时候 , 这时候的备注就是表备注
				tableComment = comment;
			}
			
			if(StringUtils.isBlank(temp))
				continue;
			
			Matcher matcher = pattern.matcher(temp);
			Matcher matcher2 = pattern2.matcher(temp);
			
			matcher = matcher.matches() ? matcher : matcher2;
			
			if(matcher.matches()) {
				comment = StringUtils.trim(matcher.group(1));
			}
			
			Matcher matcher3 = pattern3.matcher(temp);
			
			if(matcher3.matches()) {
				attributeList.add(new Attribute(StringUtils.trim(matcher3.group(2)) , comment , StringUtils.trim(matcher3.group(1))));
				comment = "";
			}
			 
		}
		
		br.close();
		
		//若是没有匹配到 , 默认取类名
		bigClassName = StringUtils.trim(bigClassName);
		String bigEntityName = bigClassName;
		String smallEntityName = MyStringUtils.lowercaseIndexLetter(bigEntityName);
		
		//把entity后缀剔除
		if(!bigClassName.equals("Entity") && bigClassName.endsWith("Entity"))
			bigClassName = bigClassName.replace("Entity", "");
			
		tableName = StringUtils.isBlank(tableName) ? bigClassName : tableName;
		tableComment = StringUtils.isBlank(tableComment) ? bigClassName : tableComment;
		tableName = StringUtils.trim(tableName);
		tableComment = StringUtils.trim(tableComment);
		
		return new MongoEntityInfo(tableName, tableComment, bigClassName, MyStringUtils.lowercaseIndexLetter(bigClassName) , bigEntityName , smallEntityName , attributeList);
	}
	
	/**
	 * @function MonggoEntity实体类信息
	 * @author 肖荣辉
	 * @date 2021年7月23日
	*/
	@Data
	@AllArgsConstructor
	public static class MongoEntityInfo implements MarkReplaceInf{
		
		//对应的表名
		private String tableName;
		
		//备注
		private String comment;
		
		//类名(处理过后缀的类名)
		private String bigClassName;
		
		//类名首字母小写
		private  String smallClassName;
		
		//entityName 首字母大写
		private String bigEntityName;
		
		//entityName 首字母小写
		private String smallEntityName;
		
		//属性信息集合
		List<Attribute> attributeList;

		@Override
		public String getTableComment() {
			return comment;
		}
		
	}
	
	
	@Data
	@AllArgsConstructor
	public static class Attribute {
		
		//属性名称
		private String name;
		
		//属性备注
		private String comment;
		
		//属性类型
		private String type;
		
		public boolean isID() {
			return "id".equals(name);
		}
		
		public boolean isDate() {
			return "Date".equals(type);
		}
		
		public boolean isString() {
			return "String".equals(type);
		}
		
	}
	
}
